<?php
/**
 * DB类
 *
 * @package util.db
 * @since 0.2
 */
class IDb extends IObject {
	const TYPE_INT = 1;
	const TYPE_LONG = 2;
	const TYPE_BOOL = 3;
	const TYPE_LOB = 4;
	const TYPE_NULL = 5;
	const TYPE_STR = 6;
	const TYPE_FLOAT = 7;
	const TYPE_DATE = 8;
	const TYPE_TIME = 9;
	const TYPE_TIMESTAMP = 10;
	
	const FETCH_ASSOC = 2;
	const FETCH_NUM = 3;
	const FETCH_BOTH = 4;
	const FETCH_OBJ = 5;
	const FETCH_COLUMN = 7;
	
	private static $drivers = array(
		"if_mysql" => "IDbMysqlDriver",
		"if_mysqli" => "IDbMysqliDriver",
		"mysql" => "IDbPdoMysql",
		"mssql" => "IDbPdoMssql"
	);
	
	private static $fetchModes = array(
		self::FETCH_ASSOC, 
		self::FETCH_NUM, 
		self::FETCH_BOTH, 
		self::FETCH_OBJ, 
		self::FETCH_COLUMN  
	);
	
	/**
	 * 取得驱动
	 * 
	 * 目前支持 
	 * - if_mysql - I-F封装的mysql驱动
	 * - if_mysqli - I-F封装的mysqli驱动
	 * - mysql - pdo_mysql支持
	 * - mssql -pdo_mssql支持
	 *
	 * @param string $name 驱动名
	 * @param array $config 配置
	 * @return IDbDriver
	 * @since 0.2
	 */
	public static function getDriver($name, $config) {
		$_name = strtolower($name);
		if (array_key_exists($_name, self::$drivers)) {
			$driverName = self::$drivers[$_name];
			return new $driverName($config);
		}
		throw new IDbException("unsupported driver '{$name}'");
	}
	
	/**
	 * 判断FETCH_MODE是否被支持
	 * 
	 * 目前支持IDb::FETCH_*
	 *
	 * @param integer $fetchMode 取的记录行的方式
	 * @return boolean
	 * @since 0.2
	 */
	public static function isSupportedFetchMode($fetchMode) {
		return in_array($fetchMode, self::$fetchModes, true);
	}
}

?>